/**
 * 上传oss
 */
var oss = require('ali-oss');
var fs = require('fs');
const { join, sep } = require('path');
const packageFile = JSON.parse(fs.readFileSync('./package.json'));

// oss账户信息
var client = oss({
  accessKeyId: 'LTAI5tLHrA5K4tBGkoYR6TJT',
  accessKeySecret: '8u4TgUFWcrgvV1lljcsYCG1JgbVKEK',
  bucket: 'gstor-default-workspace-cn-shanghai-c218770b',
  region: 'oss-cn-shanghai',
});

// 上传目录
var input = join(__dirname, '/dist');

// 项目名称
var proejctName = 'web-microApp-module-basic'

// 上传后路径
var output = `${proejctName}/dist`;

// 上传文件
function send(dir, output) {
  let files = fs.readdirSync(dir);
  files.forEach((item, index) => {
    let fPath = join(dir, item);
    let stat = fs.statSync(fPath);
    if (stat.isDirectory() === true) {
      send(fPath, join(output, item));
    }
    if (stat.isFile() === true) {
      console.log(fPath);
      let target = join(output, item)
        .split(sep)
        .join('/');
      client.put(target, fPath);
    }
  });
}

async function init() {
  await client.put(
    `${proejctName}/`,
    new Buffer(''),
  );
  const result = await client.put(
    `${proejctName}/`,
    new Buffer(''),
  );
  send(input, output);
}

init();
